{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting keras\n",
      "  Using cached https://files.pythonhosted.org/packages/5e/10/aa32dad071ce52b5502266b5c659451cfd6ffcbf14e6c8c4f16c0ff5aaab/Keras-2.2.4-py2.py3-none-any.whl\n",
      "Collecting keras-applications>=1.0.6 (from keras)\n",
      "  Using cached https://files.pythonhosted.org/packages/90/85/64c82949765cfb246bbdaf5aca2d55f400f792655927a017710a78445def/Keras_Applications-1.0.7-py2.py3-none-any.whl\n",
      "Requirement already satisfied: numpy>=1.9.1 in /usr/local/envs/py3env/lib/python3.5/site-packages (from keras) (1.14.0)\n",
      "Collecting keras-preprocessing>=1.0.5 (from keras)\n",
      "  Using cached https://files.pythonhosted.org/packages/c0/bf/0315ef6a9fd3fc2346e85b0ff1f5f83ca17073f2c31ac719ab2e4da0d4a3/Keras_Preprocessing-1.0.9-py2.py3-none-any.whl\n",
      "Requirement already satisfied: pyyaml in /usr/local/envs/py3env/lib/python3.5/site-packages (from keras) (3.13)\n",
      "Requirement already satisfied: six>=1.9.0 in /usr/local/envs/py3env/lib/python3.5/site-packages (from keras) (1.10.0)\n",
      "Requirement already satisfied: scipy>=0.14 in /usr/local/envs/py3env/lib/python3.5/site-packages (from keras) (1.0.0)\n",
      "Requirement already satisfied: h5py in /usr/local/envs/py3env/lib/python3.5/site-packages (from keras) (2.7.1)\n",
      "Installing collected packages: keras-applications, keras-preprocessing, keras\n",
      "Successfully installed keras-2.2.4 keras-applications-1.0.7 keras-preprocessing-1.0.9\n"
     ]
    }
   ],
   "source": [
    "!pip3 install keras"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This Notebook shows implementation of SSN Stacker."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "BUCKET_NAME = \"msil_raw\"\n",
    "FOLDER_NAME = \"training_data\"\n",
    "XGBSTACKER = \"stack_xgb_data.csv\"\n",
    "LSTMSTACKER = \"stack_lstm_data.csv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/envs/py3env/lib/python3.5/site-packages/sklearn/cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n",
      "  \"This module will be removed in 0.20.\", DeprecationWarning)\n",
      "/usr/local/envs/py3env/lib/python3.5/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n",
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import google.datalab.storage as storage\n",
    "import pandas as pd\n",
    "from io import BytesIO\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import xgboost as xgb\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "import time\n",
    "from datetime import datetime\n",
    "from scipy import integrate\n",
    "import pickle\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Activation\n",
    "from keras import optimizers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# setting up the parameters\n",
    "plt.rcParams[\"figure.figsize\"] = (10, 10)\n",
    "pd.set_option(\"display.max_rows\", 200)\n",
    "pd.set_option(\"display.max_columns\", 200)\n",
    "pd.set_option(\"precision\", 15)\n",
    "sns.set_style(\"darkgrid\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>y</th>\n",
       "      <th>yhat</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>93.0</td>\n",
       "      <td>92.999488800764084</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>93.0</td>\n",
       "      <td>92.998987704515457</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>93.0</td>\n",
       "      <td>92.998491972684860</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>93.0</td>\n",
       "      <td>92.997980743646622</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>93.0</td>\n",
       "      <td>92.997486114501953</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      y                yhat\n",
       "0  93.0  92.999488800764084\n",
       "1  93.0  92.998987704515457\n",
       "2  93.0  92.998491972684860\n",
       "3  93.0  92.997980743646622\n",
       "4  93.0  92.997486114501953"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mybucket = storage.Bucket(BUCKET_NAME)\n",
    "data_csv = mybucket.object(FOLDER_NAME + \"/\" + XGBSTACKER)\n",
    "\n",
    "uri = data_csv.uri\n",
    "%gcs read --object $uri --variable data\n",
    "\n",
    "stack_01 = pd.read_csv(BytesIO(data))\n",
    "stack_01.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>y</th>\n",
       "      <th>yhat</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>93.0</td>\n",
       "      <td>92.997296408284456</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>93.0</td>\n",
       "      <td>92.994592540198937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>93.0</td>\n",
       "      <td>92.991889250697554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>93.0</td>\n",
       "      <td>92.989186197984964</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>93.0</td>\n",
       "      <td>92.986482355510788</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      y                yhat\n",
       "0  93.0  92.997296408284456\n",
       "1  93.0  92.994592540198937\n",
       "2  93.0  92.991889250697554\n",
       "3  93.0  92.989186197984964\n",
       "4  93.0  92.986482355510788"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mybucket = storage.Bucket(BUCKET_NAME)\n",
    "data_csv = mybucket.object(FOLDER_NAME + \"/\" + LSTMSTACKER)\n",
    "\n",
    "uri = data_csv.uri\n",
    "%gcs read --object $uri --variable data\n",
    "\n",
    "stack_02 = pd.read_csv(BytesIO(data))\n",
    "stack_02.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "length of XGB Predictions is  2203313\n",
      "length of LSTM Predictions is  3032534\n"
     ]
    }
   ],
   "source": [
    "print(\"length of XGB Predictions is  {}\".format(len(stack_01)))\n",
    "print(\"length of LSTM Predictions is  {}\".format(len(stack_02)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# making both dataset of same len\n",
    "stack_02 = stack_02[:2203313]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>XGB_pred</th>\n",
       "      <th>LSTM_pred</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1346797</th>\n",
       "      <td>54.042679941654214</td>\n",
       "      <td>62.650685081863784</td>\n",
       "      <td>54.466497418140818</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>572358</th>\n",
       "      <td>51.111947709321967</td>\n",
       "      <td>53.181052082311361</td>\n",
       "      <td>51.027956732713278</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1866146</th>\n",
       "      <td>35.333653455972659</td>\n",
       "      <td>51.040432356670493</td>\n",
       "      <td>33.492111107775180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1171115</th>\n",
       "      <td>83.556290346384046</td>\n",
       "      <td>83.632466423278672</td>\n",
       "      <td>82.343033023102677</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1272661</th>\n",
       "      <td>83.300452572107318</td>\n",
       "      <td>84.680283159762624</td>\n",
       "      <td>82.998774052755095</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2130054</th>\n",
       "      <td>89.399169260263463</td>\n",
       "      <td>89.393875659769407</td>\n",
       "      <td>89.399999999999977</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1723166</th>\n",
       "      <td>63.656396609544757</td>\n",
       "      <td>65.485749542294073</td>\n",
       "      <td>60.313712030667617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1045753</th>\n",
       "      <td>79.769128417968730</td>\n",
       "      <td>74.544420389831060</td>\n",
       "      <td>79.870736540089752</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2057520</th>\n",
       "      <td>61.463809067010892</td>\n",
       "      <td>66.063826253963640</td>\n",
       "      <td>59.460799055816771</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1613779</th>\n",
       "      <td>85.640096795558932</td>\n",
       "      <td>85.282950522657487</td>\n",
       "      <td>86.019164949813955</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   XGB_pred           LSTM_pred               label\n",
       "1346797  54.042679941654214  62.650685081863784  54.466497418140818\n",
       "572358   51.111947709321967  53.181052082311361  51.027956732713278\n",
       "1866146  35.333653455972659  51.040432356670493  33.492111107775180\n",
       "1171115  83.556290346384046  83.632466423278672  82.343033023102677\n",
       "1272661  83.300452572107318  84.680283159762624  82.998774052755095\n",
       "2130054  89.399169260263463  89.393875659769407  89.399999999999977\n",
       "1723166  63.656396609544757  65.485749542294073  60.313712030667617\n",
       "1045753  79.769128417968730  74.544420389831060  79.870736540089752\n",
       "2057520  61.463809067010892  66.063826253963640  59.460799055816771\n",
       "1613779  85.640096795558932  85.282950522657487  86.019164949813955"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stack_02.columns = [\"actual2\", \"LSTM_pred\"]\n",
    "stack_01.columns = [\"actual\", \"XGB_pred\"]\n",
    "\n",
    "df = pd.concat((stack_01, stack_02), axis = 1).reset_index(drop = True)\n",
    "df = df.drop(columns = [\"actual2\"])\n",
    "\n",
    "df = df[[\"XGB_pred\", \"LSTM_pred\", \"actual\"]]\n",
    "df.columns = [\"XGB_pred\", \"LSTM_pred\", \"label\"]\n",
    "df.sample(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "XGB_pred     0\n",
       "LSTM_pred    0\n",
       "label        0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = df[[\"XGB_pred\", \"LSTM_pred\"]].values\n",
    "y = df[\"label\"].values.reshape(-1, 1)\n",
    "\n",
    "mms_1 = MinMaxScaler(feature_range = (0, 1))\n",
    "mms_2 = MinMaxScaler(feature_range = (0, 1))\n",
    "mms_1 = mms_1.fit(x)\n",
    "mms_2 = mms_2.fit(y)\n",
    "pickle.dump(mms_1, open(\"ssn_scaler_x.pickle.dat\", \"wb\"))\n",
    "pickle.dump(mms_2, open(\"ssn_scaler_y.pickle.dat\", \"wb\"))\n",
    "\n",
    "x=  mms_2.transform(x)\n",
    "y = mms_2.transform(y)\n",
    "\n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.25, random_state = 42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = Sequential()\n",
    "model.add(Dense(128, input_dim = x_train.shape[1], kernel_initializer = 'normal', activation = 'relu'))\n",
    "model.add(Dense(16, kernel_initializer = 'normal', activation = 'relu'))\n",
    "model.add(Dense(1, kernel_initializer = 'normal'))\n",
    "# Compile model\n",
    "model.compile(loss='mean_squared_error', optimizer=\"adam\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "1652484/1652484 [==============================] - 100s 61us/step - loss: 0.0011\n",
      "Epoch 2/10\n",
      "1652484/1652484 [==============================] - 103s 62us/step - loss: 6.4276e-04\n",
      "Epoch 3/10\n",
      "1652484/1652484 [==============================] - 102s 62us/step - loss: 6.1744e-04\n",
      "Epoch 4/10\n",
      "1652484/1652484 [==============================] - 99s 60us/step - loss: 6.0474e-04\n",
      "Epoch 5/10\n",
      "1652484/1652484 [==============================] - 100s 60us/step - loss: 5.9337e-04\n",
      "Epoch 6/10\n",
      "1652484/1652484 [==============================] - 100s 60us/step - loss: 5.5266e-04\n",
      "Epoch 7/10\n",
      "1652484/1652484 [==============================] - 101s 61us/step - loss: 5.5421e-04\n",
      "Epoch 8/10\n",
      "1652484/1652484 [==============================] - 93s 56us/step - loss: 5.2650e-04\n",
      "Epoch 9/10\n",
      "1652484/1652484 [==============================] - 101s 61us/step - loss: 5.2130e-04\n",
      "Epoch 10/10\n",
      "1652484/1652484 [==============================] - 98s 59us/step - loss: 4.9639e-04\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x7feef537cef0>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(x_train, y_train, epochs=10, batch_size=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictions = model.predict(x_test)\n",
    "predictions = mms_2.inverse_transform(predictions)\n",
    "y_test = mms_2.inverse_transform(y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictions = predictions.ravel()\n",
    "y_test = y_test.ravel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_df = pd.DataFrame({\n",
    "  \"y\": y_test,\n",
    "  \"yhat\": predictions\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>y</th>\n",
       "      <th>yhat</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>462879</th>\n",
       "      <td>83.917281984139620</td>\n",
       "      <td>82.828643798828125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>454178</th>\n",
       "      <td>72.429146470143010</td>\n",
       "      <td>70.737236022949219</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>550137</th>\n",
       "      <td>64.799999999999997</td>\n",
       "      <td>65.131294250488281</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22096</th>\n",
       "      <td>89.872184440870271</td>\n",
       "      <td>89.372619628906250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>423203</th>\n",
       "      <td>61.197558209820180</td>\n",
       "      <td>64.281372070312500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7391</th>\n",
       "      <td>34.547774969806085</td>\n",
       "      <td>35.281394958496094</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>480516</th>\n",
       "      <td>90.474506471885420</td>\n",
       "      <td>90.730033874511719</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>413620</th>\n",
       "      <td>80.566038708233791</td>\n",
       "      <td>81.444915771484375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>343872</th>\n",
       "      <td>72.570940811048615</td>\n",
       "      <td>72.817901611328125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>323251</th>\n",
       "      <td>44.071349805958732</td>\n",
       "      <td>43.463050842285156</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         y                yhat\n",
       "462879  83.917281984139620  82.828643798828125\n",
       "454178  72.429146470143010  70.737236022949219\n",
       "550137  64.799999999999997  65.131294250488281\n",
       "22096   89.872184440870271  89.372619628906250\n",
       "423203  61.197558209820180  64.281372070312500\n",
       "7391    34.547774969806085  35.281394958496094\n",
       "480516  90.474506471885420  90.730033874511719\n",
       "413620  80.566038708233791  81.444915771484375\n",
       "343872  72.570940811048615  72.817901611328125\n",
       "323251  44.071349805958732  43.463050842285156"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result_df.sample(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.223739796964447"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(((result_df[\"y\"] - result_df[\"yhat\"])**2).mean())**0.5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Saving the Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model is saved !!!\n"
     ]
    }
   ],
   "source": [
    "# serialize model to JSON\n",
    "model_json = model.to_json()\n",
    "with open(\"ann_test.json\", \"w\") as json_file:\n",
    "    json_file.write(model_json)\n",
    "# serialize weights to HDF5\n",
    "model.save_weights(\"ann_test.h5\")\n",
    "print(\"Model is saved !!!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Copying file://model_lstm_stack.json [Content-Type=application/json]...\n",
      "/ [1 files][  1.5 KiB/  1.5 KiB]                                                \n",
      "Operation completed over 1 objects/1.5 KiB.                                      \n"
     ]
    }
   ],
   "source": [
    "!gsutil cp 'model_lstm_stack.json' 'gs://msil_raw/training_data/model_lstm_stack_final.json'"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
